﻿using System.ServiceModel;
using System.Windows;
using Omniscient.Foundation;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.TimeTracker.BusinessModel.Security;

namespace Omniscient.TimeTracker
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            System.Net.ServicePointManager.ServerCertificateValidationCallback += CheckValidationResult;
            // Create new object container
            ApplicationManager.Current.ObjectContainer = new ObjectContainer();

            // Register WCF SecurityService
            ChannelFactory<ISecurityService> securityFactory;
            string endpointName = "Omniscient.TimeTracker.Server.SecurityService";
            securityFactory = new ChannelFactory<ISecurityService>(endpointName);
            ApplicationManager.Current.ObjectContainer.Register<ISecurityService>(securityFactory.CreateChannel());

            // Assign the shell
            ApplicationManager.Current.Shell = new Shell();

            ApplicationManager.Current.StartApplication();
        }

        private bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return (certificate.Subject.Contains("WMSvc-CL-T057-083CN") || certificate.Subject.Contains("localhost"));
        } 
    }
}
